package com.kalvin.kvf.modules.ks.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kalvin.kvf.common.utils.ShiroKit;
import com.kalvin.kvf.modules.hxy.entity.Account;
import com.kalvin.kvf.modules.hxy.mapper.AccountMapper;
import com.kalvin.kvf.modules.ks.entity.KSCookie;
import com.kalvin.kvf.modules.ks.service.KSCookieService;
import com.kalvin.kvf.modules.sys.entity.User;
import com.kalvin.kvf.modules.sys.mapper.UserMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.kalvin.kvf.common.controller.BaseController;
import com.kalvin.kvf.common.dto.R;


import java.util.List;


/**
 * <p>
 * 快手登录信息的cookie 前端控制器
 * </p>
 * @since 2023-11-16 13:54:01
 */
@RestController
@RequestMapping("ks/cookie")
public class KSCookieController extends BaseController {

    @Autowired
    private KSCookieService ksCookieService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AccountMapper accountMapper;

    @RequiresPermissions("ks:cookie:index")
    @GetMapping("index")
    public ModelAndView index() {
        return new ModelAndView("ks/cookie");
    }

    @GetMapping(value = "edit")
    public ModelAndView edit(Long id) {
        ModelAndView mv = new ModelAndView("ks/cookie_edit");
        KSCookie cookie;
        if (id == null) {
            cookie = new KSCookie();
        } else {
            cookie = ksCookieService.getById(id);
        }
        mv.addObject("editInfo", cookie);
        return mv;
    }

    @GetMapping(value = "list/data")
    public R listData(KSCookie cookie) {
        Page<KSCookie> page = ksCookieService.listCookiePage(cookie);
        return R.ok(page);
    }

    @RequiresPermissions("ks:cookie:add")
    @PostMapping(value = "add")
    public R add(KSCookie cookie) throws Exception {
        cookie.setPhone(ShiroKit.getUser().getUsername());
        ksCookieService.save(cookie);
        return R.ok();
    }

    @RequiresPermissions("ks:cookie:del")
    @PostMapping(value = "batchdel")
    public R batchdel(@RequestParam("ids") List<Long> ids) {
        ksCookieService.removeByIds(ids);
        return R.ok();
    }

    @RequiresPermissions("ks:cookie:edit")
    @PostMapping(value = "edit")
    public R edit(KSCookie cookie) {
        ksCookieService.updateById(cookie);
        return R.ok();
    }

    @RequiresPermissions("ks:cookie:del")
    @PostMapping(value = "del/{id}")
    public R del(@PathVariable Long id) {
        ksCookieService.removeById(id);
        return R.ok();
    }

    @GetMapping(value = "get/{id}")
    public R get(@PathVariable Long id) {
        return R.ok(ksCookieService.getById(id));
    }

}

